﻿package {
	import flash.net.*;
	import flash.events.*;
	public class Playlist {

		private var xmlData:XML;
		private var pla:Player;
		
		private var rdy:Boolean;
		private var _artist:String;
		private var _album:String;
	
		private var xmlLoader:URLLoader = new URLLoader();
		private var ready:Function;

		public function Playlist(url:URLRequest) {
			xmlLoader.addEventListener(Event.COMPLETE,
				function(e:Event) {
					xmlData = new XML(e.target.data);
				});
			xmlLoader.load(url);
		}
		
		public function setReadyListener(list:Function) {
			this.ready = list;
		}
		
		public function setPlayer(pla:Player):void{
			this.pla = pla;
		}
		
		public function get artist():String {
			return _artist;
		}
		
		public function set artist(art:String):void{
			_artist = art;
		}
		
		public function set album(alb:String):void{
			_album = alb;
		}
		
		public function get album():String {
			return _album;
		}
		
		public function getFilePath(trackTitle:String):String{
			var tmpXML:XMLList = xmlData.artist.(@name == artist).album.(@name == album).track.(@name == trackTitle).filepath;
			return "" + tmpXML;
		}
		
		public function playlist():Array{
			var tmpXML:XMLList = xmlData.artist.(@name == artist).album.(@name == album).track.@name;
			var retArray:Array = new Array(tmpXML.length());
			for(var i:int; i < tmpXML.length(); i++){
				retArray[i] = tmpXML[i];				
			}
			return retArray;
		}
		
		public function albumlist():Array{
			var tmpXML:XMLList = xmlData.artist.(@name == artist).album.@['name'];
			var retArray:Array = new Array(tmpXML.length());
			for(var i:int; i < tmpXML.length(); i++){
				retArray[i] = tmpXML[i];				
			}
			trace("Titles array (al): " + retArray + ":: len: " + retArray.length);
			return retArray;
		}
		
		public function relatedArtists():Array{
			return new Array(1);			
		}
		
		public function relatedAlbums():Array{
			return new Array(1);			
		}
	}
}